/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnlSelectServer.java
 *
 * Created on Mar 23, 2011, 8:13:22 AM
 */

package Client.GUI;

import Client.Net.*;
import java.util.*;
import java.util.Map.*;
import javax.swing.*;

/**
 *
 */
public class pnlSelectServer extends javax.swing.JPanel {

    /** Creates new form pnlSelectServer */
    protected ICommunication m_Communication;
    /** Creates new form pnlSelectServer */
    public pnlSelectServer(ICommunication communication)
    {
        initComponents();
        m_Communication = communication;
        setSize(getPreferredSize());
        //lstServers.removeAll();
        DefaultListModel mdl = (DefaultListModel)lstServers.getModel();
        mdl.clear();
        try
        {
          // GameServer lijst opvragen
          Map<String, String> servers = m_Communication.getService().getGameServers();
          if (servers == null)
          {
              m_Communication.log("WARNING: kan serverlijst niet opvragen.");
              lblOpmerking.setText("List is not available.\r\n");
          }
          else
          {
              Iterator<Entry<String, String>> it = servers.entrySet().iterator();
              while (it.hasNext())
              {
                  // Voeg GameServers to aan GameServer lijst in GUI.
                  Entry<String, String> server = it.next();

                  mdl.addElement("Server: " + server.getKey() + " on ip: " + server.getValue());
              }
          }
        }
        catch (Exception ex)
        {
            m_Communication.logException(ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstServers = new javax.swing.JList();
        btnSelect = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblOpmerking = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(420, 400));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Server list, select a server");

        lstServers.setModel(new javax.swing.DefaultListModel());
        lstServers.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstServersValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstServers);

        btnSelect.setText("Select");
        btnSelect.setEnabled(false);
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        btnCancel.setText("Back");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(271, Short.MAX_VALUE)
                .addComponent(btnSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 2, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnCancel)
                        .addComponent(lblOpmerking, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(0, 2, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(215, Short.MAX_VALUE)
                .addComponent(btnSelect)
                .addGap(92, 92, 92))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 11, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(btnCancel)
                    .addGap(40, 40, 40)
                    .addComponent(lblOpmerking, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 12, Short.MAX_VALUE)))
        );

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 380, 330));
    }// </editor-fold>//GEN-END:initComponents

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        try
        {
            // Verbinding maken met gameserver / Selecteren gameserver
            if(lstServers.getSelectedValue() != null)
            {
                String val = (String)lstServers.getSelectedValue();
                String[] parts = val.split(":");
                int port = Integer.parseInt(parts[parts.length -1]);
                String ip = parts[parts.length -2];
                if (!m_Communication.getService().connect(ip.trim(), port))
                {
                    m_Communication.log("WARNING: kan geen verbinding maken met gameserver.");
                    lblOpmerking.setText("Cannot connect to gameserver\r\n");
                }
                else
                {
                    m_Communication.SetContent(new pnlGameList(m_Communication));
                    lblOpmerking.setText("You are connected to the gameserver!\r\n");
                }
            }
            else
            {
                lblOpmerking.setText("No server selected!\r\n");
            }
        }
        catch (Exception ex)
        {
            m_Communication.logException(ex);
        }
}//GEN-LAST:event_btnSelectActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        m_Communication.SetContent(new pnlLogin(m_Communication));
}//GEN-LAST:event_btnCancelActionPerformed

    private void lstServersValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstServersValueChanged
        btnSelect.setEnabled(lstServers.getSelectedIndex() >= 0);
    }//GEN-LAST:event_lstServersValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblOpmerking;
    private javax.swing.JList lstServers;
    // End of variables declaration//GEN-END:variables

}
